package com.ansion.baseui.base.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

import com.ansion.baseui.R;


/**
 * Description : Section Header
 * FontSize : 16
 * LineSpacing : 2dp
 * FontWeight : 500 (Medium)
 */
public class Heading4TextView extends androidx.appcompat.widget.AppCompatTextView {

    private final float mFontSize = 16f;
    private final float mLineSpacing = 2f;
    private final int mFontWeight = 510;

    public Heading4TextView(Context context) {
        this(context, null);
    }

    public Heading4TextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Heading4TextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    public void init(@Nullable AttributeSet attrs) {
        setTextSize(mFontSize);
        setLineSpacing(mLineSpacing, 1.0f);

        int typefaceStyle = 0;

        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TextStyle);
            typefaceStyle = typedArray.getInt(R.styleable.TextStyle_typefaceStyle, 0);
            typedArray.recycle();
        }

        try {
            Typeface typeface;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
                typeface = Typeface.create(getFamily(typefaceStyle), mFontWeight, false);
            } else {
                typeface = getFamily(typefaceStyle);
            }
            setTypeface(typeface);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    private Typeface getFamily(int typefaceStyle) {
        Typeface family = null;
        if (typefaceStyle == 0) {
            family = Typeface.DEFAULT;
        } else if (typefaceStyle == 1) {
            family = Typeface.DEFAULT_BOLD;
        } else if (typefaceStyle == 2) {
            family = Typeface.SANS_SERIF;
        } else if (typefaceStyle == 3) {
            family = Typeface.SERIF;
        } else if (typefaceStyle == 4) {
            family = Typeface.MONOSPACE;
        }
        return family == null ? Typeface.DEFAULT : family;
    }
}
